-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stream: pipeline don't destroy Duplex src before 'finish' #32966
Conversation
fcb17b5
to
229f5d2
Compare
pipeline was too agressive with destroying Duplex streams which were the first argument into pipeline. Just because it's !writable does not mean that it is safe to be destroyed, unless it has also emitted 'finish'. Fixes: nodejs#32955
lib/internal/streams/pipeline.js
Outdated
const wState = stream._writableState; | ||
|
||
const writableEnded = stream.writableEnded || | ||
(wState && wState.ended); | ||
const writableFinished = stream.writableFinished || | ||
(wState && wState.finished); | ||
|
||
const willFinish = stream.writable || | ||
(writableEnded && !writableFinished); | ||
const willEnd = stream.readable; | ||
|
||
if (err || !final || !stream.readable) { | ||
destroyImpl.destroyer(stream, err); | ||
if (!err) { | ||
// First | ||
if (reading && !writing && willFinish) { | ||
return callback(); | ||
} | ||
|
||
// Last | ||
if (!reading && writing && willEnd) { | ||
return callback(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: it is possible to reduce the code branches a bit:
const wState = stream._writableState; | |
const writableEnded = stream.writableEnded || | |
(wState && wState.ended); | |
const writableFinished = stream.writableFinished || | |
(wState && wState.finished); | |
const willFinish = stream.writable || | |
(writableEnded && !writableFinished); | |
const willEnd = stream.readable; | |
if (err || !final || !stream.readable) { | |
destroyImpl.destroyer(stream, err); | |
if (!err) { | |
// First | |
if (reading && !writing && willFinish) { | |
return callback(); | |
} | |
// Last | |
if (!reading && writing && willEnd) { | |
return callback(); | |
} | |
if (!err) { | |
// Check if the last stream will end: | |
if (!reading) { | |
if (writing && stream.readable) { | |
return callback(); | |
} | |
// First stream | |
} else if (!writing) { | |
if (stream.writable) { | |
return callback(); | |
} | |
const wState = stream._writableState; | |
const writableEnded = stream.writableEnded || | |
(wState && wState.ended); | |
const writableFinished = stream.writableFinished || | |
(wState && wState.finished); | |
if (writableEnded && !writableFinished) | |
return callback(); | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BridgeAR: Any specific reason? This does make it a bit harder to read in my opinon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code should IMO always only run when necessary. Right now some code is executed that is only needed in some situations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a hot path so readability should IMO come first. I think I can make a compromise 😄. PTAL.
destroyImpl.destroyer(stream, err); | ||
if (!err) { | ||
// First | ||
if (reading && !writing && willFinish) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The willFinish
part here is the significant change. Before we only checked stream.writable
.
The rest of the changes should be logically the same.
blocked pending #32954 |
closed in favor of #32968 |
pipeline was too agressive with destroying Duplex
streams which were the first argument into pipeline.
Just because it's !writable does not mean that it
is safe to be destroyed, unless it has also emitted
'finish'.
Fixes: #32955
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes